unit clsItemRegra;

interface
uses Dm, dbtables, biblio, Sysutils;
type
  TItemRegra = class(tobject)
  private
    Reg_codigo: string;
    Irg_numpgto: integer;
  public
    Irg_dias: integer;
    Irg_controle: string;
    function Gravar(p_Reg_codigo: string; p_Irg_numpgto: integer): Byte;
    function Recuperar(p_Reg_codigo: string; p_Irg_numpgto: integer): Byte;
    function Excluir(p_Reg_codigo: string; p_Irg_numpgto: integer): Byte;
    function Retornar_PM(p_Reg_codigo: string): integer;
    function Retornar_Dias(p_Reg_codigo: string; pNumPgto: Integer): integer;
    procedure Excluir_Todos(p_Reg_codigo: string);
    procedure ConsultarPorTabela(var p_q: Tquery; P_Reg_codigo: string);
  end;
implementation

{ TItemRegra }

procedure TItemRegra.ConsultarPorTabela(var p_q: Tquery;
  P_Reg_codigo: string);
begin
  p_q.SQL.Clear;
  p_q.Params.Clear;
  p_q.SQL.add('Select * from itemRegra where Reg_codigo=:K_P_Reg_Codigo order by irg_numpgto');
  p_q.ParamByName('K_P_Reg_Codigo').AsString := StrZero(P_Reg_codigo, 3);
end;

function TItemRegra.Excluir(p_Reg_codigo: string;
  p_Irg_numpgto: integer): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM ItemRegra WHERE Reg_codigo=:k_Reg_Codigo and Irg_numpgto=:K_Irg_Numpgto');
  tq.parambyname('k_reg_Codigo').asstring := strzero(p_Reg_Codigo, 3);
  tq.parambyname('k_Irg_Numpgto').Asinteger := p_Irg_Numpgto;
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TItemRegra.Excluir_Todos;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := DmInt.db.databasename;
  tq.active := false;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from ItemRegra where Reg_codigo=:a');
  tq.params[0].asstring := strzero(p_Reg_Codigo, 3);
  tq.execSQL;
  tq.close;
  tq.Free;
end;

function TItemRegra.Gravar(p_Reg_codigo: string;
  p_Irg_numpgto: integer): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM ItemRegra where Reg_Codigo=:k_Reg_Codigo and Irg_Numpgto=:k_Irg_Numpgto');
  tq.parambyname('k_Reg_Codigo').asstring := StrZero(p_Reg_codigo, 3);
  tq.parambyname('k_Irg_Numpgto').Asinteger := p_Irg_numpgto;
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO ItemRegra VALUES(:k_Reg_codigo,' +
      ':k_Irg_numpgto,' +
      ':k_Irg_Dias,');
    if AllTrim(Irg_controle) <> '' then
      tq.sql.add(':K_Irg_controle)')
    else
      tq.sql.add('null)');
  end
  else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE ItemRegra SET Irg_dias =:k_Irg_dias, ');
    if AllTrim(Irg_controle) <> '' then
      tq.sql.add('Irg_controle =:k_Irg_controle  ')
    else
      tq.sql.add('Irg_controle =null  ');
    tq.sql.add('WHERE Reg_codigo=:k_reg_codigo and Irg_numpgto=:k_Irg_Numpgto');
  end;
  tq.parambyname('k_Reg_codigo').AsString := StrZero(p_Reg_codigo, 3);
  tq.parambyname('k_Irg_Numpgto').Asinteger := p_Irg_Numpgto;
  tq.parambyname('k_Irg_dias').AsInteger := Irg_Dias;
  if AllTrim(Irg_controle) <> '' then
    tq.parambyname('k_Irg_Controle').AsString := Irg_controle;
  tq.Close;
  tq.ExecSQL;
  Result := 1;
  tq.close;
  tq.Free;

end;

function TItemRegra.Recuperar(p_Reg_codigo: string;
  p_Irg_numpgto: integer): Byte;
var
  tq: TQuery;
begin
  result := 0;
  tq := Tquery.Create(DmInt);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM ItemRegra where Reg_codigo=:k_Reg_codigo and Irg_numpgto=:k_Irg_Numpgto');
  tq.Params[0].asstring := strzero(p_Reg_codigo, 3);
  tq.params[1].AsInteger := p_Irg_Numpgto;
  tq.open;
  Reg_codigo := p_Reg_codigo;
  Irg_Numpgto := p_Irg_Numpgto;
  if tq.eof then begin
    Reg_codigo := '';
    Irg_Numpgto := 0;
    Irg_Dias := 0;
    Irg_controle := '';
  end
  else begin
    Irg_Dias := tq.fieldbyname('Irg_Dias').AsInteger;
    Irg_Controle := tq.fieldbyname('Irg_Controle').AsString;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TItemRegra.Retornar_Dias;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := DmInt.db.databasename;
  tq.active := false;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from ItemRegra where Reg_codigo=:a and Irg_NumPgto =:Num');
  tq.params[0].asstring := strzero(p_Reg_Codigo, 3);
  tq.params[1].AsInteger := pNumPgto;
  tq.open;
  Result := tq.FieldByName('Irg_Dias').AsInteger;
  tq.Close;
  tq.Free;
end;

function TItemRegra.Retornar_PM;
var
  tq: TQuery;
  dias: integer;
  num_pgto: integer;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := DmInt.db.databasename;
  tq.active := false;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from ItemRegra where Reg_codigo=:a');
  tq.params[0].asstring := strzero(p_Reg_Codigo, 3);
  tq.open;
  dias := 0;
  num_pgto := 0;
  while not tq.eof do begin
    dias := dias + tq.fieldbyname('Irg_Dias').asinteger;
    num_pgto := num_pgto + 1;
    tq.next;
  end;
  if num_pgto > 0 then
    result := iv(dias / num_pgto)
  else
    result := 0;
  tq.close;
  tq.Free;
end;

end.
